package jdk6;

import java.util.List;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.junit.Test;

public class Scripting {

	public static void main(String[] args) throws Exception {
		// 引擎管理
		ScriptEngineManager manager = new ScriptEngineManager();

		// 支持的脚本有哪些
		List<ScriptEngineFactory> engineFactories = manager.getEngineFactories();
		if (engineFactories.size() == 0) {
			System.out.println("本JVM尚不支持任何脚本引擎");
			return;
		}

		System.out.println("本JVM支持的脚本引擎有:");
		for (ScriptEngineFactory engineFactory : engineFactories) {
			System.out.println("    引擎名称:" + engineFactory.getEngineName());
			System.out.println("\t可被ScriptEngineManager识别的名称:" + engineFactory.getNames());
			System.out.println("\t该引擎支持的脚本语言名称:" + engineFactory.getLanguageName());
			System.out.println("\t是否线程安全:" + engineFactory.getParameter("THREADING"));
			System.out.println();
		}
	}

	// 测试一
	@Test
	public void test1() throws Exception {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine scriptEngine = manager.getEngineByName("javascript");
		String exp = "a+b";
		scriptEngine.put("a", 4);
		scriptEngine.put("b", 15);
		Object result = scriptEngine.eval(exp);
		System.out.println(exp + "=" + result);
	}

	// 测试2
	@Test
	public void test2() throws Exception {
		ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
		ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("javascript");
		String exp = "2*6-(6+5)";
		Object result = scriptEngine.eval(exp);
		System.out.println(exp + "=" + result);
	}

	// 测试3
	@Test
	public void test3() throws ScriptException {
		ScriptEngineManager manager = new ScriptEngineManager();
		// 支持通过名称、文件扩展名、MIMEtype查找
		ScriptEngine engine = manager.getEngineByName("JavaScript");
		// engine = manager.getEngineByExtension("js");
		// engine = manager.getEngineByMimeType("text/javascript");
		if (engine == null) {
			throw new RuntimeException("找不到JavaScript语言执行引擎。");
		}
		engine.eval("var println =function(msg){ return msg };");
		Object result = engine.eval("println('Hello!');");

		System.out.println(result);
	}

	public static String json = "{\"code\":0,\"msg\":\"操作成功\",\"result\":{\"datas\":[{\"address\":\"菁蓉镇\",\"code\":\"101\",\"level\":\"1\",\"pcode\":\"0\",\"postcode\":\"\",\"description\":{\"name\":\"集团投资总部\",\"pcpcc\":\"\",\"cpcc\":\"TCQ3hIJR13\",\"type\":1,\"code\":\"101\",\"pcode\":\"0\"},\"pid\":0,\"telephone\":\"13012455623\",\"updateTime\":\"2018-12-24 09:53:42.0\",\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 14:58:29.0\",\"updateBy\":\"岸本齐史\",\"children\":[{\"address\":\"\",\"code\":\"1010012\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"集团办公室\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"WcNTQhRUkH\",\"type\":3,\"code\":\"1010012\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13011455214\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 15:02:41.0\",\"updateBy\":null,\"children\":[],\"contact\":\"test\",\"name\":\"集团办公室\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":3},{\"address\":\"\",\"code\":\"101008\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"投资二部\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"5u7SqPrROo\",\"type\":3,\"code\":\"101008\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13011455214\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 15:04:31.0\",\"updateBy\":null,\"children\":[],\"contact\":\"test\",\"name\":\"投资二部\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":4},{\"address\":\"\",\"code\":\"101004\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"金融财务部\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"yEyWkukYLZ\",\"type\":3,\"code\":\"101004\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13011455214\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 15:06:02.0\",\"updateBy\":null,\"children\":[],\"contact\":\"test\",\"name\":\"金融财务部\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":5},{\"address\":\"\",\"code\":\"101005\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"集团财务部\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"LX0J7KkaiD\",\"type\":3,\"code\":\"101005\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13011455214\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 15:06:30.0\",\"updateBy\":null,\"children\":[],\"contact\":\"test\",\"name\":\"集团财务部\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":6},{\"address\":\"\",\"code\":\"101006\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"投资一部\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"zPzenFeexQ\",\"type\":3,\"code\":\"101006\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13011455214\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 15:12:17.0\",\"updateBy\":null,\"children\":[],\"contact\":\"test\",\"name\":\"投资一部\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":7},{\"address\":\"\",\"code\":\"101007\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"信息企划部\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"XrZB0UKEjw\",\"type\":3,\"code\":\"101007\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13011455214\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 15:13:12.0\",\"updateBy\":null,\"children\":[],\"contact\":\"test\",\"name\":\"信息企划部\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":8},{\"address\":\"\",\"code\":\"1010013\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"人力资源部\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"WI0Psd8HsV\",\"type\":3,\"code\":\"1010013\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13011455214\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 15:13:43.0\",\"updateBy\":null,\"children\":[],\"contact\":\"test\",\"name\":\"人力资源部\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":9},{\"address\":\"\",\"code\":\"1010014\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"风险审计部\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"FODPGKZ3CM\",\"type\":3,\"code\":\"1010014\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13011455214\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 15:19:17.0\",\"updateBy\":null,\"children\":[],\"contact\":\"test\",\"name\":\"风险审计部\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":10},{\"address\":\"\",\"code\":\"101009\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"外派财务负责人\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"0OYgQXosrI\",\"type\":3,\"code\":\"101009\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13011455214\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 15:19:57.0\",\"updateBy\":null,\"children\":[],\"contact\":\"test\",\"name\":\"外派财务负责人\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":11},{\"address\":\"\",\"code\":\"1010010\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"金融投资部与IR\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"FK4hqosngn\",\"type\":3,\"code\":\"1010010\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13011455214\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 15:26:02.0\",\"updateBy\":null,\"children\":[],\"contact\":\"test\",\"name\":\"金融投资部与IR\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":12},{\"address\":\"\",\"code\":\"1010011\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"信息部\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"tF6fSXtJDp\",\"type\":3,\"code\":\"1010011\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13011455214\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-12 15:31:59.0\",\"updateBy\":null,\"children\":[],\"contact\":\"test\",\"name\":\"信息部\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":13},{\"address\":\"\",\"code\":\"101002\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"正大天晴药业集团股份有限公司\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"Sj52cYJPKx\",\"type\":2,\"code\":\"101002\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13012455623\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-13 11:08:13.0\",\"updateBy\":null,\"children\":[],\"contact\":\"zhengda\",\"name\":\"正大天晴药业集团股份有限公司\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":14},{\"address\":\"\",\"code\":\"101003\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"北京泰德制药股份有限公司\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"RZrbus1V5c\",\"type\":2,\"code\":\"101003\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13012455623\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-13 11:08:24.0\",\"updateBy\":null,\"children\":[],\"contact\":\"zhengda\",\"name\":\"北京泰德制药股份有限公司\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":15},{\"address\":\"\",\"code\":\"101001\",\"level\":\"2\",\"pcode\":\"101\",\"postcode\":\"\",\"description\":{\"name\":\"南京正大天晴制药有限公司\",\"pcpcc\":\"TCQ3hIJR13\",\"cpcc\":\"8ztt7KTWTJ\",\"type\":2,\"code\":\"101001\",\"pcode\":\"101\"},\"pid\":2,\"telephone\":\"13012455623\",\"updateTime\":null,\"createBy\":\"zhengda\",\"createTime\":\"2018-12-13 11:08:37.0\",\"updateBy\":null,\"children\":[],\"contact\":\"zhengda\",\"name\":\"南京正大天晴制药有限公司\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":16}],\"contact\":\"张三\",\"name\":\"集团投资总部\",\"tenantId\":\"c9jvxcc9s4vr732z\",\"id\":2}]}}";

	// 测试4
	@Test
	public void test4() throws Exception {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("JavaScript");
		// ScriptEngine engine = manager.getEngineByExtension("js");
		// ScriptEngine engine = manager.getEngineByMimeType("text/javascript");
		engine.eval("function a(json,args,args2){return JSON.stringify(JSON.parse(json)[args][args2][0]);}");
		Object invokeFunction = ((Invocable) engine).invokeFunction("a", json, "result", "datas");
		System.out.println(invokeFunction);

	}

}
